﻿using System.Data;

namespace U8DllPack.Controller
{
    public class CustomDataTable
    {
        /// <summary>
        /// 创建用于下拉框的数据源DataTable
        /// </summary>
        /// <param name="idtype">id类型</param>
        /// <param name="ids">id数组</param>
        /// <param name="texts">text数组</param>
        /// <returns>返回DataTable</returns>
        public static DataTable CreateDT(Type idtype, object?[] ids, string[] texts)
        {
            DataTable dt = new();
            if ((idtype.IsGenericType) && (idtype.GetGenericTypeDefinition() == typeof(Nullable<>)))
            {
                idtype = idtype.GetGenericArguments()[0];
            }
            DataColumn dc = new("Id", idtype, string.Empty);
            dt.Columns.Add(dc);
            dc = new DataColumn("Text", typeof(string), string.Empty);
            dt.Columns.Add(dc);

            for (int i = 0; i < ids.Length; i++)
            {
                DataRow dr = dt.NewRow();
                if (ids[i] == null || Convert.ToString(ids[i]).IsNullOrWhiteSpace())
                {
                    dr["Id"] = DBNull.Value;
                }
                else
                {
                    dr["Id"] = Convert.ChangeType(ids[i], idtype);
                }
                dr["Text"] = texts[i];
                dt.Rows.Add(dr);
            }
            return dt;
        }
        /// <summary>
        /// 获取自字义内存表文本值
        /// </summary>
        /// <param name="dt">自字义DataTable</param>
        /// <param name="id">编号值</param>
        /// <returns></returns>
        public static string? Get_CusDT_Text(DataTable dt, object id)
        {
            string? ret = string.Empty;
            if (id.IsDBNullToNull() == null) return ret;
            if (dt == null || dt.Rows.Count == 0) return ret;
            DataRow[] dr = dt.Select("Id=" + id);
            if (dr.Length > 0)
            {
                ret = Convert.ToString(dr[0]["Text"]);
            }
            return ret;
        }
        public static DataTable DT_U8ver()
        {
            return CreateDT(typeof(byte?), [null, 0, 1, 2], [string.Empty, "所有版本", "13.0", "16.0"]);
        }
        /// <summary>
        /// 返回链接文件内存空表
        /// </summary>
        /// <returns></returns>
        public static DataTable ReturnDPDT()
        {
            return new DataTable
            {
                Columns =
                {
                    {
                        "u8ver",
                        typeof(byte)
                    },
                    {
                        "respath",
                        typeof(string)
                    },
                    {
                        "filepath",
                        typeof(string)
                    },
                    {
                        "name",
                        typeof(string)
                    },
                    {
                        "ver",
                        typeof(string)
                    },
                    {
                        "desc",
                        typeof(string)
                    }
                }
            };
        }
    }
}
